package com.xindaoapp.happypet;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.xindaoapp.happypet.ui.CommunityFragment;
import com.xindaoapp.happypet.ui.MainFragment;
import com.xindaoapp.happypet.ui.MyMessageFragment;
import com.xindaoapp.happypet.ui.PersonalCenterFragment_new;

public class StartActivity extends FragmentActivity {

    // ����FragmentTabHost����
    private FragmentTabHost mTabHost;
    // ����һ������
    private LayoutInflater layoutInflater;

    // �������������Fragment����
    private final Class fragmentArray[] = {MyMessageFragment.class, CommunityFragment.class,
            PersonalCenterFragment_new.class, MainFragment.class};

    // ������������Ű�ťͼƬ
    private final int mImageViewArray[] = {R.drawable.bottom_msg_selector,
            R.drawable.bottom_group_selector, R.drawable.bottom_home_selector,
            R.drawable.bottom_mine_selector};

    // Tabѡ�������
    private final String mTextviewArray[] = {"首页", "产品目录", "联系方式", "留言板"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_tab_layout);

        initView();
    }

    /**
     * ��ʼ�����
     */
    private void initView() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        System.out.println("------------------------屏幕分辨率为:" + dm.widthPixels + " * "
                + dm.heightPixels);
        // ʵ��ֶ���
        layoutInflater = LayoutInflater.from(this);

        // ʵ��TabHost���󣬵õ�TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        // �õ�fragment�ĸ���
        int count = fragmentArray.length;

        for (int i = 0; i < count; i++) {
            // Ϊÿһ��Tab��ť����ͼ�ꡢ���ֺ�����
            TabSpec tabSpec =
                    mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
            // ��Tab��ť��ӽ�Tabѡ���
            mTabHost.addTab(tabSpec, fragmentArray[i], null);

            // ����Tab��ť�ı���
            // mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);

        }
    }

    /**
     * ��Tab��ť����ͼ�������
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray[index]);

        TextView textView = (TextView) view.findViewById(R.id.textview);

        textView.setText(mTextviewArray[index]);

        return view;
    }


}
